package com.changgou.business.listener;

import okhttp3.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <dl>
 * <dd>描述: ~节点业务逻辑实现</dd>
 * <dd>创建时间：  10:26 2020/4/5</dd>
 * <dd>创建人： zz</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2020/4/5      guodong       1.0        1.0 Version
 * </pre>
 * </dl>
 */
@Component
@RabbitListener(queues = "ad_update_queue")
public class AdListener {

    private Logger log = LoggerFactory.getLogger(AdListener.class);

    @RabbitHandler
    public void receiveMsg(String msg){
        if(StringUtils.isNotEmpty(msg)){
            log.info("接收到大广告更新数据 ： {}", msg);
            //1.组装url请求地址
            String url = "http://192.168.200.128/ad_update?position=" + msg;
            OkHttpClient okHttpClient = new OkHttpClient();
            //构建请求对象
            Request request = new Request.Builder().url(url).build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    log.error("大广告更新请求发送失败 ： {}", e.getMessage());
                }

                @Override
                public void onResponse(Call call, Response response){
                    log.info("大广告更新请求发送成功 ： {}", response.message());
                }
            });
        }
    }
}
